#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using TVC.Modules.CustomControl;

#endregion

namespace TVC.Modules.Ref
{
    public partial class FrmContractDetail : Form
    {
        public MainForm MainForm { get; set; }
        DataTable _dtOrderReq;
        DataTable _dtBudget;

        public DataTable DtBudget
        {
            get { return _dtBudget; }
            set { _dtBudget = value; }
        }
        public DataTable DtOrderReq
        {
            get { return _dtOrderReq; }
            set { _dtOrderReq = value; }
        }
        //protected System.Globalization.CultureInfo mobjCustomCulture;
        //protected System.Globalization.CultureInfo mobjSavedCulture = null;
        public FrmContractDetail()
        {
            InitializeComponent();
            ID.Visible = false;
            //if (mobjCustomCulture == null)
            //{
            //    mobjCustomCulture = System.Globalization.CultureInfo.CreateSpecificCulture(System.Threading.Thread.CurrentThread.CurrentCulture.Name);
            //    string[] myDateTimePatterns = new string[] { "dd/MM/yyyy", "dd/MM/yyyy", "dd/MM/yyyy" }; //{ "ddd dd/MM/yy", "ddd dd/MM/yyyy", "dd/MM/yyyy" };
            //    mobjCustomCulture.DateTimeFormat.SetAllDateTimePatterns(myDateTimePatterns, 'd');

            //}

            //System.Threading.Thread.CurrentThread.CurrentCulture = mobjCustomCulture;
            //VWGContext.Current.CurrentUICulture = mobjCustomCulture;
        }

        private void btAddDetail_Click(object sender, EventArgs e)
        {
            DataSource.Rows.InsertAt(DataSource.NewRow(), 0);
            InsertTopItem(DataSource.Rows[0]);
        }
        /// <summary>
        /// Adds row to ListView
        /// </summary>
        private void AddItem(DataRow info)
        {
            // ListViewItem objItem = null;
            ListViewPanelItem objItem = CreateItem(info);
            this.mobjListView.Items.Add(objItem);

        }

        /// <summary>
        /// Adds row to ListView
        /// </summary>
        private void InsertTopItem(DataRow info)
        {
            // ListViewItem objItem = null;
            ListViewPanelItem objItem = CreateItem(info);
            this.mobjListView.Items.Insert(0, objItem);

            if (objItem.Panel.Visible = !objItem.Panel.Visible)
            {
                ListViewItem.ListViewSubControlItem sub = objItem.SubItems[1] as ListViewItem.ListViewSubControlItem;
                if (sub != null)
                {
                    Button objButton = sub.Control as Button;
                    if (objButton != null)
                    {
                        objButton.Text = "Commit";
                    }
                }
            }
        }

        private ListViewPanelItem CreateItem(DataRow info)
        {
            ListViewControlPanel objPanel = new ListViewControlPanel(MainForm);
            objPanel.MainForm = this.MainForm;
            objPanel.SetOrderReqtSource(DtOrderReq);
            objPanel.SetBudgetSource(DtBudget);
            ListViewPanelItem objItem = new ListViewPanelItem(objPanel, info["ID"].ToString());
            objItem.Tag = objPanel;
            objPanel.Visible = false;
            Gizmox.WebGUI.Forms.Button objButton = new Gizmox.WebGUI.Forms.Button();
            objButton.Click += new EventHandler(objButton_Click);
            objButton.Tag = objItem;
            objButton.Text = "Edit";
            objButton.Height = 22;
            objButton.FlatStyle = FlatStyle.Flat;
            // objItem = this.mobjListView.Items.Add(objPanel, info.ID.ToString());
            objPanel.SetData(info);
            objItem.SubItems.Add(objButton);
            objItem.SubItems.Add(info["LimitAmt"] == DBNull.Value ? "" : Convert.ToDouble(info["LimitAmt"]).ToString("N2"));
            objItem.SubItems.Add(info["LimitAmtOther"] == DBNull.Value ? "" : Convert.ToDouble(info["LimitAmtOther"]).ToString("N0"));
            objItem.SubItems.Add(info["CIT"] == DBNull.Value ? "" : Convert.ToDouble(info["CIT"]).ToString("N2"));
            objItem.SubItems.Add(info["VAT"] == DBNull.Value ? "" : Convert.ToDouble(info["VAT"]).ToString("N0"));
            objItem.SubItems.Add(info["Addendum"].ToString());
            objItem.SubItems.Add(info["SignDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["SignDate"]).ToString("dd/MM/yyyy"));
            objItem.SubItems.Add(info["EffectiveDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["EffectiveDate"]).ToString("dd/MM/yyyy"));
            objItem.SubItems.Add(info["ExpiryDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["ExpiryDate"]).ToString("dd/MM/yyyy"));
            objItem.SubItems.Add(info["YearBudgetCode"].ToString());
            objItem.SubItems.Add(info["OrderReq"].ToString());
            objItem.SubItems.Add(info["Status"].ToString());
            objItem.SubItems.Add(info["aType"].ToString());
            objItem.SubItems.Add(info["Remark1"].ToString());
            objItem.SubItems.Add(info["Remark2"].ToString());
            objItem.SubItems.Add(info["Remark3"].ToString());
            objItem.SubItems.Add(info["MCApproval"].ToString());
            objItem.SubItems.Add(info["ActionRequired"].ToString());
            return objItem;
        }

        void objButton_Click(object sender, EventArgs e)
        {
            Gizmox.WebGUI.Forms.Button objButton = sender as Gizmox.WebGUI.Forms.Button;
            if (objButton != null)
            {
                ListViewPanelItem objPanelItem = objButton.Tag as ListViewPanelItem;
                if (objPanelItem != null)
                {
                    if (objPanelItem.Panel.Visible = !objPanelItem.Panel.Visible)
                    {
                        objButton.Text = "Commit";
                    }
                    else
                    {
                        objButton.Text = "Edit";
                        ListViewControlPanel objPanel = objPanelItem.Panel as ListViewControlPanel;
                        if (objPanel != null)
                        {
                            if (objPanel.IsPanelDirty)
                            {
                                objPanel.SetPrevValues();
                                var info = objPanel.ItemDataBinding;
                                //objItem.SubItems.Add(objButton);
                                objPanelItem.SubItems[2].Text = info["LimitAmt"] == DBNull.Value ? "" : Convert.ToDouble(info["LimitAmt"]).ToString("N2");
                                objPanelItem.SubItems[3].Text = (info["LimitAmtOther"] == DBNull.Value ? "" : Convert.ToDouble(info["LimitAmtOther"]).ToString("N0"));
                                objPanelItem.SubItems[4].Text = (info["CIT"] == DBNull.Value ? "" : Convert.ToDouble(info["CIT"]).ToString("N2"));
                                objPanelItem.SubItems[5].Text = (info["VAT"] == DBNull.Value ? "" : Convert.ToDouble(info["VAT"]).ToString("N0"));
                                objPanelItem.SubItems[6].Text = (info["Addendum"].ToString());
                                objPanelItem.SubItems[7].Text = (info["SignDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["SignDate"]).ToString("dd/MM/yyyy"));
                                objPanelItem.SubItems[8].Text = (info["EffectiveDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["EffectiveDate"]).ToString("dd/MM/yyyy"));
                                objPanelItem.SubItems[9].Text = (info["ExpiryDate"] == DBNull.Value ? "" : Convert.ToDateTime(info["ExpiryDate"]).ToString("dd/MM/yyyy"));
                                objPanelItem.SubItems[10].Text = (info["YearBudgetCode"].ToString());
                                objPanelItem.SubItems[11].Text = (info["OrderReq"].ToString());
                                objPanelItem.SubItems[12].Text = (info["Status"].ToString());
                                objPanelItem.SubItems[13].Text = (info["aType"].ToString());
                                objPanelItem.SubItems[14].Text = (info["Remark1"].ToString());
                                objPanelItem.SubItems[15].Text = (info["Remark2"].ToString());
                                objPanelItem.SubItems[16].Text = (info["Remark3"].ToString());
                                objPanelItem.SubItems[17].Text = (info["MCApproval"].ToString());
                                objPanelItem.SubItems[18].Text = (info["ActionRequired"].ToString());
                            }
                        }
                    }
                }
            }
        }
        internal void SetReadOnly(bool p)
        {
            tbarGrid.Visible = !p;
            colEdit.Visible = !p;
        }

        public DataTable DataSource { get; set; }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmContractDetail_Load(object sender, EventArgs e)
        {
            //mobjListView.DataSource = DataSource;
            foreach (DataRow r in DataSource.Rows)
            {
                AddItem(r);
            }
        }

        private void btDelDetail_Click(object sender, EventArgs e)
        {
            for (int i = mobjListView.CheckedItems.Count - 1; i >= 0; i--)
            {
                var item = mobjListView.CheckedItems[i] as ListViewPanelItem;
                if (item != null)
                {
                    ListViewControlPanel objPanel = item.Panel as ListViewControlPanel;
                    DataSource.Rows.Remove(objPanel.ItemDataBinding);
                    mobjListView.Items.Remove(item);
                }
            }
        }
    }
}